package com.example.demo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.splashscreen.SplashScreen.KeepOnScreenCondition
import androidx.core.splashscreen.SplashScreen.OnExitAnimationListener
import androidx.core.splashscreen.SplashScreenViewProvider
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.example.featuresdk.navigation.INavigationMediator
import com.example.featuresdk.navigation.sharedNestedNavigationGraph
import com.example.featuresdk.theme.DemoTheme
import com.example.featuresdk.view.SearchScreen
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject
    lateinit var navigationMediator: INavigationMediator

    @ExperimentalFoundationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //https://developer.android.google.cn/guide/topics/ui/splash-screen
        //https://developer.android.google.cn/guide/topics/ui/splash-screen/migrate
        val splashScreenVisible = mutableStateOf(true)
        val splashScreen = installSplashScreen()
        splashScreen.setKeepVisibleCondition(MyKeepVisibleCondition(splashScreenVisible))
        splashScreen.setOnExitAnimationListener(MyExitAnimationListener())

        //Delay 200ms to simulate latency to initialize the home screen due to network calls
        CoroutineScope(SupervisorJob()).launch {
            delay(200)
            splashScreenVisible.value = false
        }

//        resources.apply {
//            updateConfiguration(bootstrap.apply {
////                this.setLocale(Locale.TRADITIONAL_CHINESE)
////                this.setLocale(Locale.SIMPLIFIED_CHINESE)
//                setLocale(Locale.ENGLISH)
//            },displayMetrics)
//        }

        //update the context here according to configuration changes
        navigationMediator.features().forEach {
            it.context = this
        }

        setContent {

            val featureSDKs = remember {
                navigationMediator.entitledFeatures()
            }

            DemoTheme {
                if (featureSDKs.isNotEmpty()) {
                    val navController = rememberNavController()
                    val startDestination = remember { featureSDKs[0].featureKey() }
                    NavHost(navController = navController, startDestination = startDestination) {
                        //appNavigationGraph(navController, featureSDKs)
                        featureSDKs.forEach { featureSdk ->
                            featureSdk.buildNavigationGraph(this, navController)
                        }
                        sharedNestedNavigationGraph(navController, featureSDKs)
                    }
                    SearchScreen()
                }
            }
        }
    }
}

class MyKeepVisibleCondition(private val splashScreenVisible: State<Boolean>): KeepOnScreenCondition{
    override fun shouldKeepOnScreen(): Boolean {
        return splashScreenVisible.value
    }
}

class MyExitAnimationListener:  OnExitAnimationListener{
    override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
        //Important: Remove the SplashScreen's view from the view hierarchy at the end of animation.
        splashScreenViewProvider.remove()
        //TODO: there is a bug that the splash screen will show on the background after app's rotation changes
//        val slideRight = ObjectAnimator.ofFloat(splashScreenViewProvider.view,
//            View.TRANSLATION_X.name,
//             0f, splashScreenViewProvider.view.width.toFloat())
//
//        slideRight.interpolator = AnticipateInterpolator()
//        slideRight.duration = 1000L
//
//        //Important: Remove the SplashScreen's view from the view hierarchy at the end of animation.
//        slideRight.doOnEnd { splashScreenViewProvider.remove() }
//
//        // Run animation.
//        slideRight.start()
    }
}
